package com.magicborrow.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.magicborrow.Constants;
import com.magicborrow.R;
import com.magicborrow.utils.UserTools;
import com.magicborrow.utils.VolleyTool;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by River on 6/15/16.
 */
public class MyAccountActivity extends BaseActivity{

    private TextView money;
    private Button applyWithDraw;
    private String amount = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_account_activity);
        initView();
        initData();
    }

    private void initView(){
        money = (TextView)findViewById(R.id.money);
        applyWithDraw = (Button)findViewById(R.id.applyWithDraw);
        applyWithDraw.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                if(!"".equals(amount)){
                    intent.putExtra("amount",amount);
                    intent.setClass(MyAccountActivity.this,ApplyWithDrawActivity.class);
                    MyAccountActivity.this.startActivity(intent);
                }
            }
        });
    }

    private void initData(){
        Map<String,String> params = new HashMap<>();
        params.put("acc_token", UserTools.getUser(this).getAcc_token());
        VolleyTool.get(Constants.MY_ACCOUNT_URL, params, new VolleyTool.HTTPListener() {
            @Override
            public <T> void onResponse(T t, int requestCode) {
                try {
                    JSONObject json = new JSONObject(t.toString());
                    int code = json.getInt("code");
                    String message = json.getString("message");
                    if(code >=0 ){
                        JSONObject obj = new JSONObject(json.getString("data"));
                        amount = obj.getString("amount");
                        money.setText("￥"+amount+"元");
                    }else{
                        showShortMsg(message);
                    }
                } catch (JSONException e) {
                   // e.printStackTrace();
                }
            }

            @Override
            public void onErrorResponse(VolleyError error, int requestCode) {
                showShortMsg("服务器暂时无法链接");
            }
        },0,null);
    }

}
